package com.software.loginregister.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.software.loginregister.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class LikesAdapter extends RecyclerView.Adapter<LikesAdapter.ViewHolder> {
    private static final String TAG = "LikesAdapter";
    private Context context;
    private List<Map<String, Object>> likes;

    public LikesAdapter(Context context, List<Map<String, Object>> likes) {
        this.context = context;
        this.likes = likes != null ? likes : new ArrayList<>();
    }

    public void updateData(List<Map<String, Object>> newLikes) {
        Log.d(TAG, "updateData: Updating with " +
                (newLikes != null ? newLikes.size() : 0) + " items");
        this.likes = newLikes != null ? newLikes : new ArrayList<>();
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        try {
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_likes, parent, false);
            Log.d(TAG, "Created new ViewHolder");
            return new ViewHolder(view);
        } catch (Exception e) {
            Log.e(TAG, "Error creating ViewHolder", e);
            // 创建一个空的视图作为后备
            View fallbackView = new View(context);
            return new ViewHolder(fallbackView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        try {
            if (position >= likes.size()) {
                Log.e(TAG, "Position out of bounds: " + position);
                return;
            }

            Map<String, Object> like = likes.get(position);
            Log.d(TAG, "Binding position " + position + ": " + like);

            // 安全地设置用户名
            String nickname = (String) like.get("nickname");
            if (holder.tvUsername != null) {
                holder.tvUsername.setText(nickname != null ? nickname : "未知用户");
            } else {
                Log.e(TAG, "tvUsername is null for position " + position);
            }

            // 安全地设置时间
            if (holder.tvLikeTime != null) {
                String likeTime = (String) like.get("likeTime");
                holder.tvLikeTime.setText(formatTime(likeTime));
            } else {
                Log.e(TAG, "tvLikeTime is null for position " + position);
            }

            // 安全地设置头像
            if (holder.ivAvatar != null) {
                String avatarUrl = (String) like.get("avatarUrl");
                if (!TextUtils.isEmpty(avatarUrl)) {
                    Glide.with(context)
                            .load(avatarUrl)
                            .circleCrop()
                            .placeholder(R.drawable.ic_default_avatar)
                            .error(R.drawable.ic_default_avatar)
                            .into(holder.ivAvatar);
                } else {
                    holder.ivAvatar.setImageResource(R.drawable.ic_default_avatar);
                }
            } else {
                Log.e(TAG, "ivAvatar is null for position " + position);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error binding view holder at position " + position, e);
        }
    }
    @Override
    public int getItemCount() {
        return likes != null ? likes.size() : 0;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAvatar;
        TextView tvUsername;
        TextView tvLikeTime;

        ViewHolder(View itemView) {
            super(itemView);
            // 确保 ID 与布局文件中的一致
            ivAvatar = itemView.findViewById(R.id.iv_my_work_avatars);
            tvUsername = itemView.findViewById(R.id.tv_my_work_usernames);
            tvLikeTime = itemView.findViewById(R.id.tv_my_work_like_time);
        }
    }

    private String formatTime(String timeStr) {
        if (timeStr == null) return "未知时间";
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(timeStr);
            return date != null ? outputFormat.format(date) : "未知时间";
        } catch (ParseException e) {
            Log.e(TAG, "Error parsing date: " + timeStr, e);
            return "未知时间";
        }
    }
}